package business.entity;

/*
 * Author cxs
 * Date 2013-05-27
 * 用于验证user的用户名和密码是否符合给定的规则
 */
public class VerifyUser {
	public static final int max_name_len = 20;
	public static final int min_passwd_len = 4;
	public static final int max_passwd_len = 18;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	/*
	 * 判断名字是否合法,若合法则返回null,
	 * 否则返回不合法的原因
	 */
	public String verifyUserName(){
		//若user为null,则不用判断
		if(user == null) return null;
		String name = user.getName();
		String rules = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]{0," + max_name_len + "}$";
		//若名字为空
		if(name == null || "".equals(name)){
			return "用户名不能为空";
		}else if(name.length() > max_name_len){
			return "用户名过长";
		}else if(!name.matches(rules)){
			return "用户名只能为汉字,英文和数字的组合";
		}
		return null;
	}
	
	/*
	 * 判断密码是否合法,若合法则返回null,
	 * 否则返回不合法的原因
	 */
	public String verifyUserPasswd(){
		//若user为null,则不用判断
		if(user == null) return null;
		String pwd = user.getPasswd();
		String rules = "[0-9A-Za-z]{0,"+max_passwd_len+"}";
		
		if(pwd == null || "".equals(pwd)){
			return "密码不能为空";
		}else if (pwd.length() > max_passwd_len){
			return "密码过长";
		}else if (pwd.length() < min_passwd_len){
			return "密码过短";
		}else if(!pwd.matches(rules)){
			return "用户名只能为英文和数字的组合";
		}
		return null;
	}
	
	private User user = null;
}
